/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.account.Key.DataSourceBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.datasource.DataSourceManager;
/*     */ import com.zimbra.cs.db.DbDataSource;
/*     */ import com.zimbra.cs.db.DbPop3Message;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.admin.type.DataSourceType;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ModifyDataSource
/*     */   extends MailDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException, SoapFaultException
/*     */   {
/*  47 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  48 */     Provisioning prov = Provisioning.getInstance();
/*  49 */     Account account = getRequestedAccount(zsc);
/*     */     
/*  51 */     if (!canModifyOptions(zsc, account)) {
/*  52 */       throw ServiceException.PERM_DENIED("can not modify options");
/*     */     }
/*     */     
/*     */ 
/*  56 */     Element eDataSource = CreateDataSource.getDataSourceElement(request);
/*  57 */     DataSourceType type = DataSourceType.fromString(eDataSource.getName());
/*     */     
/*  59 */     String id = eDataSource.getAttribute("id");
/*  60 */     DataSource ds = prov.get(account, Key.DataSourceBy.id, id);
/*  61 */     if (ds == null) {
/*  62 */       throw ServiceException.INVALID_REQUEST("Unable to find data source with id=" + id, null);
/*     */     }
/*     */     
/*  65 */     Map<String, Object> dsAttrs = new HashMap();
/*  66 */     String value = eDataSource.getAttribute("name", null);
/*  67 */     if (value != null)
/*  68 */       dsAttrs.put("zimbraDataSourceName", value);
/*  69 */     value = eDataSource.getAttribute("isEnabled", null);
/*  70 */     if (value != null) {
/*  71 */       dsAttrs.put("zimbraDataSourceEnabled", LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool("isEnabled")));
/*     */     }
/*  73 */     value = eDataSource.getAttribute("l", null);
/*  74 */     if (value != null) {
/*  75 */       Mailbox mbox = getRequestedMailbox(zsc);
/*  76 */       CreateDataSource.validateFolderId(account, mbox, eDataSource, type);
/*  77 */       dsAttrs.put("zimbraDataSourceFolderId", value);
/*     */     }
/*     */     
/*  80 */     value = eDataSource.getAttribute("host", null);
/*  81 */     if ((value != null) && (!value.equals(ds.getHost()))) {
/*  82 */       dsAttrs.put("zimbraDataSourceHost", value);
/*     */     }
/*     */     
/*  85 */     value = eDataSource.getAttribute("port", null);
/*  86 */     if (value != null)
/*  87 */       dsAttrs.put("zimbraDataSourcePort", value);
/*  88 */     value = eDataSource.getAttribute("connectionType", null);
/*  89 */     if (value != null) {
/*  90 */       dsAttrs.put("zimbraDataSourceConnectionType", value);
/*     */     }
/*  92 */     value = eDataSource.getAttribute("username", null);
/*  93 */     if ((value != null) && (!value.equals(ds.getUsername()))) {
/*  94 */       dsAttrs.put("zimbraDataSourceUsername", value);
/*     */     }
/*     */     
/*  97 */     value = eDataSource.getAttribute("password", null);
/*  98 */     if (value != null) {
/*  99 */       dsAttrs.put("zimbraDataSourcePassword", value);
/*     */     }
/* 101 */     value = eDataSource.getAttribute("leaveOnServer", null);
/* 102 */     if (value != null) {
/* 103 */       if (type != DataSourceType.pop3) {
/* 104 */         String msg = String.format("%s only allowed for %s data sources", new Object[] { "leaveOnServer", "pop3" });
/*     */         
/* 106 */         throw ServiceException.INVALID_REQUEST(msg, null);
/*     */       }
/* 108 */       boolean newValue = eDataSource.getAttributeBool("leaveOnServer");
/* 109 */       if (newValue != ds.leaveOnServer()) {
/* 110 */         dsAttrs.put("zimbraDataSourceLeaveOnServer", LdapUtil.getLdapBooleanString(newValue));
/*     */         
/* 112 */         Mailbox mbox = getRequestedMailbox(zsc);
/* 113 */         DbPop3Message.deleteUids(mbox, ds.getId());
/* 114 */         DbDataSource.deleteAllMappings(ds);
/*     */       }
/*     */     }
/*     */     
/* 118 */     value = eDataSource.getAttribute("pollingInterval", null);
/* 119 */     if (value != null) {
/* 120 */       dsAttrs.put("zimbraDataSourcePollingInterval", value);
/*     */     }
/*     */     
/*     */ 
/* 124 */     String importClass = eDataSource.getAttribute("importClass", DataSourceManager.getDefaultImportClass(type));
/* 125 */     if (importClass != null) {
/* 126 */       dsAttrs.put("zimbraDataSourceImportClassName", importClass);
/*     */     }
/*     */     
/* 129 */     processCommonOptionalAttrs(dsAttrs, eDataSource);
/*     */     
/* 131 */     prov.modifyDataSource(account, id, dsAttrs);
/*     */     
/* 133 */     Element response = zsc.createElement(MailConstants.MODIFY_DATA_SOURCE_RESPONSE);
/*     */     
/* 135 */     return response;
/*     */   }
/*     */   
/*     */   public static void processCommonOptionalAttrs(Map<String, Object> dsAttrs, Element eDataSource)
/*     */     throws ServiceException
/*     */   {
/* 141 */     String value = eDataSource.getAttribute("emailAddress", null);
/* 142 */     if (value != null) {
/* 143 */       dsAttrs.put("zimbraDataSourceEmailAddress", value);
/*     */     }
/* 145 */     value = eDataSource.getAttribute("useAddressForForwardReply", null);
/* 146 */     if (value != null) {
/* 147 */       dsAttrs.put("zimbraDataSourceUseAddressForForwardReply", LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool("useAddressForForwardReply", false)));
/*     */     }
/*     */     
/* 150 */     value = eDataSource.getAttribute("defaultSignature", null);
/* 151 */     if (value != null) {
/* 152 */       dsAttrs.put("zimbraPrefDefaultSignatureId", value);
/*     */     }
/* 154 */     value = eDataSource.getAttribute("forwardReplySignature", null);
/* 155 */     if (value != null) {
/* 156 */       dsAttrs.put("zimbraPrefForwardReplySignatureId", value);
/*     */     }
/* 158 */     value = eDataSource.getAttribute("fromDisplay", null);
/* 159 */     if (value != null) {
/* 160 */       dsAttrs.put("zimbraPrefFromDisplay", value);
/*     */     }
/* 162 */     value = eDataSource.getAttribute("replyToAddress", null);
/* 163 */     if (value != null) {
/* 164 */       dsAttrs.put("zimbraPrefReplyToAddress", value);
/*     */     }
/* 166 */     value = eDataSource.getAttribute("replyToDisplay", null);
/* 167 */     if (value != null)
/* 168 */       dsAttrs.put("zimbraPrefReplyToDisplay", value);
/* 169 */     value = eDataSource.getAttribute("pollingInterval", null);
/* 170 */     if (value != null) {
/* 171 */       dsAttrs.put("zimbraDataSourcePollingInterval", value);
/*     */     }
/* 173 */     Iterator<Element> attrs = eDataSource.elementIterator("a");
/* 174 */     if ((attrs != null) && (attrs.hasNext())) {
/* 175 */       ArrayList<String> attrList = new ArrayList();
/* 176 */       while (attrs.hasNext()) {
/* 177 */         attrList.add(((Element)attrs.next()).getText());
/*     */       }
/* 179 */       dsAttrs.put("zimbraDataSourceAttribute", attrList.toArray(new String[0]));
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/ModifyDataSource.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */